package com.weilan.blog.picture.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weilan.blog.base.enums.EStatus;
import com.weilan.blog.base.serviceImpl.SuperServiceImpl;
import com.weilan.blog.common.global.MessageConf;
import com.weilan.blog.common.global.SQLConf;
import com.weilan.blog.common.global.SysConf;
import com.weilan.blog.picture.entity.Picture;
import com.weilan.blog.picture.entity.PictureCategory;
import com.weilan.blog.picture.mapper.PictureCategoryMapper;
import com.weilan.blog.picture.service.FileService;
import com.weilan.blog.picture.service.PictureService;
import com.weilan.blog.picture.vo.PictureCategoryVO;
import com.weilan.blog.picture.service.PictureCategoryService;
import com.weilan.blog.picture.utils.WebUtil;
import com.weilan.blog.utils.ResultUtil;
import com.weilan.blog.utils.StringUtils;
import com.weilan.blog.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @description:
 * @author: 孙 峰
 * @time: 2020/10/10 15:33
 */
@Service
public class PictureCategoryServiceImpl extends SuperServiceImpl<PictureCategoryMapper, PictureCategory> implements PictureCategoryService {


    @Autowired
    private PictureService pictureService;

    @Autowired
    private FileService fileService;

    @Autowired
    private WebUtil webUtil;

    @Override
    public IPage<PictureCategory> getPageList(PictureCategoryVO pictureCategoryVO) {

        QueryWrapper<PictureCategory> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pictureCategoryVO.getKeyword()) && !StringUtils.isEmpty(pictureCategoryVO.getKeyword().trim())) {
            queryWrapper.like(SQLConf.NAME, pictureCategoryVO.getKeyword().trim());
        }

        if (pictureCategoryVO.getIsShow() != null) {
            queryWrapper.eq(SQLConf.IS_SHOW, SysConf.ONE);
        }
        Page<PictureCategory> page = new Page<>();
        page.setCurrent(pictureCategoryVO.getCurrentPage());
        page.setSize(pictureCategoryVO.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
        queryWrapper.orderByDesc(SQLConf.SORT);
        IPage<PictureCategory> pageList = page(page, queryWrapper);
        List<PictureCategory> list = pageList.getRecords();
        final StringBuffer fileUids = new StringBuffer();
        list.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getFileUid())) {
                fileUids.append(item.getFileUid() + SysConf.FILE_SEGMENTATION);
            }
        });
        String pictureResult = null;
        Map<String, String> pictureMap = new HashMap<>();
        if (fileUids != null) {
            pictureResult = this.fileService.getPicture(fileUids.toString(), SysConf.FILE_SEGMENTATION);
        }
        List<Map<String, Object>> picList = webUtil.getPictureMap(pictureResult);

        picList.forEach(item -> {
            pictureMap.put(item.get(SysConf.UID).toString(), item.get(SysConf.URL).toString());
        });

        for (PictureCategory item : list) {
            //获取图片
            if (StringUtils.isNotEmpty(item.getFileUid())) {
                List<String> pictureUidsTemp = StringUtils.changeStringToString(item.getFileUid(), SysConf.FILE_SEGMENTATION);
                List<String> pictureListTemp = new ArrayList<>();
                pictureUidsTemp.forEach(picture -> {
                    pictureListTemp.add(pictureMap.get(picture));
                });
                item.setPhotoList(pictureListTemp);
            }
        }
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public String addPictureCategory(PictureCategoryVO pictureCategoryVO) {
        PictureCategory pictureCategory = new PictureCategory();
        UpdateUtil.copyNullProperties(pictureCategoryVO, pictureCategory);
        pictureCategory.setStatus(EStatus.ENABLE);
        pictureCategory.setCreateTime(LocalDateTime.now());
        pictureCategory.setUpdateTime(LocalDateTime.now());
        pictureCategory.insert();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String editPictureCategory(PictureCategoryVO pictureCategoryVO) {
        PictureCategory pictureCategory = getById(pictureCategoryVO.getUid());
        UpdateUtil.copyNullProperties(pictureCategoryVO, pictureCategory);
        pictureCategory.setUpdateTime(LocalDateTime.now());
        pictureCategory.updateById();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String deletePictureCategory(String pictureCategoryUid) {
        //判断分类下是否有图片 有图片不能杀出
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
        queryWrapper.eq(SQLConf.PICTURE_CATEGORY_UID, pictureCategoryUid);
        int count = pictureService.count(queryWrapper);

        if (count > 0) {
            return ResultUtil.result(SysConf.ERROR, MessageConf.PICTURE_UNDER_THIS_SORT);
        }
        PictureCategory pictureCategory = getById(pictureCategoryUid);
        pictureCategory.setStatus(EStatus.DISABLED);
        pictureCategory.setUpdateTime(LocalDateTime.now());
        pictureCategory.updateById();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.DELETE_SUCCESS);
    }

    /**
     * 置顶分类
     *
     * @param uid
     * @return
     */
    @Override
    public String stickPictureCategory(String uid) {
        PictureCategory pictureCategory = getById(uid);
        //  //查找出最大的那一个
        QueryWrapper<PictureCategory> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.orderByDesc(SQLConf.SORT);
        Page<PictureCategory> page = new Page<>();
        page.setSize(1);
        page.setCurrent(0);
        IPage<PictureCategory> pageList = page(page, categoryQueryWrapper);
        PictureCategory maxCategory = pageList.getRecords().get(0);

        if (StringUtils.isEmpty(maxCategory.getUid())) {
            return ResultUtil.result(SysConf.ERROR, MessageConf.PARAM_INCORRECT);
        }
        if (maxCategory.getUid().equals(pictureCategory.getUid())) {
            return ResultUtil.result(SysConf.ERROR, MessageConf.THIS_SORT_IS_TOP);
        }
        Integer sortCount = maxCategory.getSort() + 1;
        pictureCategory.setSort(sortCount);
        pictureCategory.setUpdateTime(LocalDateTime.now());
        pictureCategory.updateById();
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.OPERATION_SUCCESS);
    }


}
